import { createApi } from 'kuan-request'
import authority from './authority'
import { login401 } from '@/utils/login'

const isDev = process.env.NODE_ENV === 'development'

export default createApi({
  // 默认配置
  createOptions: {
    baseURL: '//api.luzhongkuan.cn',
    // baseURL: isDev ? 'http://localhost:8002' : '//api.luzhongkuan.cn',
    withCredentials: true
  },
  // 动态设置headers
  getHeaders: () => {
    const token = authority.get()
    if (!token) return {}
    return { Authorization: `manager ${token}` }
  },
  // 错误处理
  handleError: (status, msg, error) => {
    console.log(status, msg, error)
    if (status === 401) login401()
  }
})
